package me.vudu.Conflict.Listeners;

import me.vudu.Conflict.ConflictPlugin;
import me.vudu.Conflict.Blocks.ArtifactBlock;
import me.vudu.Conflict.Blocks.BaseBlockGiverBlock;
import me.vudu.Conflict.Blocks.BaseSeekerBlock;

import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.getspout.spoutapi.block.SpoutBlock;
import org.getspout.spoutapi.player.SpoutPlayer;

/**
 * verarbeitet Aktionen auf VuduBrick
 * 
 * @author vwendel
 * 
 */
public class ArtifactBlockListener implements Listener {

	ConflictPlugin plugin;
	

	public ArtifactBlockListener(ConflictPlugin plugin) {
		Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
		this.plugin = plugin;
	}

	@EventHandler(priority = EventPriority.HIGH)
	public void ArtifactClicked(PlayerInteractEvent event) {
		if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
			SpoutPlayer pl = (SpoutPlayer)event.getPlayer();
			

			Block a = event.getClickedBlock();

			if (null != a) {
				if (a instanceof SpoutBlock && a.getTypeId() == ConflictPlugin.ARTIFACTBLOCKID) {
					SpoutBlock sb = (SpoutBlock) a;
					
					
					int customId = sb.getCustomBlock().getCustomId();
					
					for(ArtifactBlock af : ConflictPlugin.allArtifacts){
						if(af.getCustomId() == customId){
							af.onBlockClicked(pl.getWorld(), a.getX(), a.getY(), a.getZ(), pl);
						}
					}
					
					for(BaseSeekerBlock af : ConflictPlugin.allBaseSeekerBlocks){
						if(af.getCustomId() == customId){
							af.onBlockClicked(pl.getWorld(), a.getX(), a.getY(), a.getZ(), pl);
						}
					}
					
		
				}
				
				if (a instanceof SpoutBlock && a.getTypeId() == ConflictPlugin.BASESEEKERBLOCKID) {
					SpoutBlock sb = (SpoutBlock) a;
					
					int customId = sb.getCustomBlock().getCustomId();
					
					for(BaseSeekerBlock af : ConflictPlugin.allBaseSeekerBlocks){
						if(af.getCustomId() == customId){
							af.onBlockClicked(pl.getWorld(), a.getX(), a.getY(), a.getZ(), pl);
						}
					}
				}
				
				if (a instanceof SpoutBlock && a.getTypeId() == ConflictPlugin.BASEBLOCKID) {
					SpoutBlock sb = (SpoutBlock) a;
					int customId = sb.getCustomBlock().getCustomId();
					
					BaseBlockGiverBlock bbgb = (BaseBlockGiverBlock)ConflictPlugin.baseBlockGiverBlock;
					if(bbgb.getCustomId() == customId){
						bbgb.onBlockClicked(pl.getWorld(), a.getX(), a.getY(), a.getZ(), pl);
					}
					
					
				}
			}
		}

	}
}
